import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        // 初始化售货机
        Commodity cafe = new Commodity("Juice", 10, 5);
        Commodity sprite = new Commodity("Cola", 6, 5);
        Commodity cola = new Commodity("Tea", 5, 5);
        Commodity blackTea = new Commodity("Water", 8, 5);
        Commodity lemonade = new Commodity("Coffee", 7, 5);

        VendingMachine vendingMachine = new VendingMachine(1234);
        List<Commodity> commodityList = vendingMachine.getCommodities();
        commodityList.add(cafe);
        commodityList.add(sprite);
        commodityList.add(cola);
        commodityList.add(blackTea);
        commodityList.add(lemonade);
        for (Commodity commodity : commodityList) {
            vendingMachine.getInitiallyReserve().add(commodity.getReserve());
        }
        vendingMachine.setCommodities(commodityList);

        Scanner scanner = new Scanner(System.in);
        printVendingMachine(vendingMachine);
        while (true) {
            printSelect();
            int choose = scanner.nextInt();
            if (choose == 0) {
                System.out.println("Process finished with exit code 0");
                break;
            }
            switch (choose) {
                case 1:
                    printCommodities(vendingMachine);
                    break;
                case 2:
                    while (true) {
                        printInsertCoin();
                        int anInt = scanner.nextInt();
                        if (anInt == 0) {
                            if (vendingMachine.getMoney() > 99) {
                                vendingMachine.setMoney(99);
                            }
                            break;
                        }
                        switch (anInt) {
                            case 1:
                                vendingMachine.setMoney(vendingMachine.getMoney() + 1);
                                printMoney(vendingMachine);
                                break;
                            case 2:
                                vendingMachine.setMoney(vendingMachine.getMoney() + 2);
                                printMoney(vendingMachine);
                                break;
                            case 3:
                                vendingMachine.setMoney(vendingMachine.getMoney() + 5);
                                printMoney(vendingMachine);
                                break;
                            case 4:
                                vendingMachine.setMoney(vendingMachine.getMoney() + 10);
                                printMoney(vendingMachine);
                                break;
                            default:
                                System.out.println("Invalid choice！");
                                break;
                        }
                    }
                    break;
                case 3:
                    while (true) {
                        printProductButton(vendingMachine);
                        List<Commodity> commodities = vendingMachine.getCommodities();
                        int anInt = scanner.nextInt();
                        System.out.println(anInt);
                        if (anInt == 0) {
                            break;
                        }
                        System.out.println("You have pressed button " + commodities.get(anInt - 1).getIdChar() + ".");
                        // 计算收入
                        vendingMachine.setRevenue(vendingMachine.getRevenue() + commodities.get(anInt - 1).getPrice());

                        if (commodities.get(anInt - 1).getReserve() == 0) {
                            // 商品售空
                            System.out.println("Sold out!");
                            break;
                        }
                        commodities.get(anInt - 1).setReserve(commodities.get(anInt - 1).getReserve() - 1);
                        if (vendingMachine.getMoney() - commodities.get(anInt - 1).getPrice() < 0) {
                            // 余额不足
                            System.out.println("Insufficient balance！");
                            break;
                        }
                        vendingMachine.setMoney(vendingMachine.getMoney() - commodities.get(anInt - 1).getPrice());
                        printVendingMachine(vendingMachine, commodities.get(anInt - 1));
                    }
                    break;
                case 4:
                    printReturnButton();
                    System.out.println("$" + vendingMachine.getMoney() + " has been returned.");
                    printVendingMachine(vendingMachine);
                case 9:
                    System.out.println("(9) Opening service menu. Access code is required.\n" +
                            "Enter access code:");
                    int pwd = scanner.nextInt();
                    // 去除整数前面的0
                    String pwdStr = pwd + "";
                    int pwdRlt = Integer.parseInt(pwdStr);

                    if (vendingMachine.getPwd() != pwdRlt) {
                        System.out.println("Incorrect code!");
                        break;
                    } else {
                        System.out.println("Correct code!\n");
                        while (true) {
                            printServiceMenu();
                            int serviceChoose = scanner.nextInt();
                            if (serviceChoose == 0) {
                                System.out.println("0. Go back");
                                break;
                            }
                            switch (serviceChoose) {
                                case 1:
                                    printMachineStatus(vendingMachine);
                                    break;
                                case 2:
                                    withdrawAllMoney(vendingMachine);
                                    break;
                                case 3:
                                    refillProduct(vendingMachine, scanner);
                                    break;
                                case 4:
                                    changingProduct(vendingMachine, scanner);
                                    break;
                                default:
                                    System.out.println("Invalid choice！");
                                    break;
                            }
                        }
                    }
                default:
                    System.out.println("Invalid choice！");
                    break;
            }


        }

    }

    private static void changingProduct(VendingMachine vendingMachine, Scanner scanner) {
        System.out.println("(9-4) Which product would you like to change?");
        for (int i = 0; i < vendingMachine.getCommodities().size(); i++) {
            System.out.println((i + 1) + ". " + vendingMachine.getCommodities().get(i).getIdChar());
        }
        System.out.println("0. Go back\n");
        System.out.print("Your choice:");
        int next = scanner.nextInt();
        System.out.println("You are changing product " + vendingMachine.getCommodities().get(next - 1).getIdChar() + ".");
        System.out.print("Enter new product name: ");
        String name = scanner.next();
        while (name.length() > 20 || name.contains(" ")) {
            System.out.println("名称最多可包含20个字符，并且之间不能有空格。\n请重新输入:");
            name = scanner.next();
        }
        System.out.println(name);
        System.out.print("Enter new product price: ");
        int price = scanner.nextInt();
        System.out.println(price);
        vendingMachine.getCommodities().get(next - 1).setName(name);
        vendingMachine.getCommodities().get(next - 1).setPrice(price);
        vendingMachine.getCommodities().get(next - 1).setReserve(vendingMachine.getInitiallyReserve().get(next - 1));
        System.out.println("The new product E has been filled to full.");
    }

    // 9-3
    private static void refillProduct(VendingMachine vendingMachine, Scanner scanner) {
        System.out.println("(9-3) Which product would you like to refill?");
        for (int i = 0; i < vendingMachine.getCommodities().size(); i++) {
            System.out.println((i + 1) + ". " + vendingMachine.getCommodities().get(i).getIdChar());
        }
        System.out.println("0. Go back\n");
        System.out.print("Your choice:");
        int next = scanner.nextInt();
        Integer initiallyReserve = vendingMachine.getInitiallyReserve().get(next - 1);
        vendingMachine.getCommodities().get(next - 1).setReserve(initiallyReserve);
        System.out.println("You have refilled product " + vendingMachine.getCommodities().get(next - 1).getIdChar() + " to full.");
    }

    // 9-2
    private static void withdrawAllMoney(VendingMachine vendingMachine) {
        System.out.println("(9-2) All money is being withdrawn.");
        int allMoney = vendingMachine.getMoney() + vendingMachine.getRevenue();
        System.out.println("$" + allMoney + " is withdrawn.");
    }

    // 9-1
    private static void printMachineStatus(VendingMachine vendingMachine) {
        System.out.println("(9-1) Machine status");
        System.out.println("Amount of revenue: $" + vendingMachine.getRevenue());
        System.out.println("Amount of inserted coins: $" + vendingMachine.getMoney());
        for (Commodity commodity : vendingMachine.getCommodities()) {
            if (commodity.getReserve() != 0) {
                System.out.println(commodity.getIdChar() + commodity.getName() + "($" + commodity.getPrice() + ")(" + commodity.getReserve() + " left)");
            } else {
                System.out.println(commodity.getIdChar() + commodity.getName() + "($" + commodity.getPrice() + ")(sold out)");
            }
        }
    }

    private static void printMoney(VendingMachine vendingMachine) {
        for (Commodity commodity : vendingMachine.getCommodities()) {
            if (vendingMachine.getMoney() >= commodity.getPrice()) {
                commodity.setCanPurchase(true);
            }
        }
        printVendingMachine(vendingMachine);
    }

    // 打印主菜单
    private static void printSelect() {
        System.out.print("What would you like to do?\n" +
                " 1. Read product information\n" +
                " 2. Insert coin\n" +
                " 3. Press product button\n" +
                " 4. Press return button\n" +
                " 9. Open service menu (code required)\n" +
                " 0. Quit\n" +
                "\n" +
                "Your choice:");
    }

    // 打印售货机内商品信息   (1)
    private static void printCommodities(VendingMachine vendingMachine) {
        System.out.println("(1) The displayed products are:");
        for (Commodity commodity : vendingMachine.getCommodities()) {
            System.out.println(commodity.getIdChar() + ". " + commodity.getName() + " ($" + commodity.getPrice() + ")");
        }
    }

    // 打印显示带有可用硬币选项的子菜单     (2)
    private static void printInsertCoin() {
        System.out.println("(2) Which coin would you like to insert?\n" +
                " 1. $1\n" +
                " 2. $2\n" +
                " 3. $5\n" +
                " 4. $10\n" +
                " 0. Go back\n\n" +
                "Your choice:");
    }

    // 打印显示带有可用产品选项的子菜单     (3)
    private static void printProductButton(VendingMachine vendingMachine) {
        System.out.println("(3) Which product button would you like to press?");
        for (int i = 0; i < vendingMachine.getCommodities().size(); i++) {
            System.out.println((i + 1) + ". " + vendingMachine.getCommodities().get(i).getIdChar());
        }
        System.out.println("0. Go back");
        System.out.print("Your choice:");
    }

    // 4. Press return button
    private static void printReturnButton() {
        System.out.println("(4) Return button is pressed.");
    }

    // 9. Service menu
    private static void printServiceMenu() {
        System.out.println("(9) What would you like to do?\n" +
                " 1. Inspect machine status\n" +
                " 2. Withdraw all money\n" +
                " 3. Refill product\n" +
                " 4. Change product\n" +
                " 0. Go back\n" +
                "\n" +
                "Your choice:");
    }

    // 打印售货机信息
    private static void printVendingMachine(VendingMachine vendingMachine) {
        System.out.println("*---------------------------*");
        System.out.println("|     Vending   Machine     |");
        System.out.println("*---------------------------*");
        System.out.print("|   ");
        for (int i = 0; i < vendingMachine.getCommodities().size(); i++) {
            System.out.print(vendingMachine.getCommodities().get(i).getIdChar());
            if (i == vendingMachine.getCommodities().size() - 1) {
                System.out.print("   ");
            } else {
                System.out.print("    ");
            }
        }
        System.out.println("|");

        System.out.print("|");
        for (Commodity commodity : vendingMachine.getCommodities()) {
            System.out.print("  ");
            System.out.printf("$%2d", commodity.getPrice());
        }
        System.out.println("  |");

        System.out.print("|");
        for (Commodity commodity : vendingMachine.getCommodities()) {
            System.out.print("  [");
            System.out.print(commodity.isCanPurchase());
            System.out.print("]");
        }
        System.out.println("  |");

        System.out.println("*---------------------------*");

        System.out.printf("|                    [$%2d]  |", vendingMachine.getMoney());
        System.out.println();

        System.out.println("|                           |");
        System.out.println("|           [===]           |");
        System.out.println("*---------------------------*");
    }


    private static void printVendingMachine(VendingMachine vendingMachine, Commodity someone) {
        System.out.println("*---------------------------*");
        System.out.println("|     Vending   Machine     |");
        System.out.println("*---------------------------*");
        System.out.print("|   ");
        for (int i = 0; i < vendingMachine.getCommodities().size(); i++) {
            System.out.print(vendingMachine.getCommodities().get(i).getIdChar());
            if (i == vendingMachine.getCommodities().size() - 1) {
                System.out.print("   ");
            } else {
                System.out.print("    ");
            }
        }
        System.out.println("|");

        System.out.print("|");
        for (Commodity commodity : vendingMachine.getCommodities()) {
            System.out.print("  ");
            System.out.printf("$%2d", commodity.getPrice());
        }
        System.out.println("  |");

        System.out.print("|");
        for (Commodity commodity : vendingMachine.getCommodities()) {
            System.out.print("  [");
            System.out.print(commodity.isCanPurchase());
            System.out.print("]");
        }
        System.out.println("  |");

        System.out.println("*---------------------------*");

        System.out.printf("|                    [$%2d]  |", vendingMachine.getMoney());
        System.out.println();

        System.out.println("|                           |");

        System.out.println("|           [=" + someone.getIdChar() + "=]           |");

        System.out.println("*---------------------------*");
    }
}
